﻿using POC.Configuration;

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using wlog.Shell.IO;
using wlog.Shell.Storage;

namespace wlog.Shell
{
    public class CommandController
    {
        public const string INVALIDCOMMAND = "INVALIDCOMMAND";
        bool isLocalDevelopment;
        public CommandController(bool local)
        {
            isLocalDevelopment = local;
        }
        public ICommand CreateCommand(string[] args)
        {
            ICommand consoleCommand = null;
            string commandName = (args.Length < 1) ? INVALIDCOMMAND : args[0].ToLower().Trim('"');
            switch (commandName)
            {
                case "download":
                case "webclient":
                case "httpget":
                    var httpcommand = new WebClientCommand();
                    httpcommand.Setup(args);
                    consoleCommand = httpcommand;
                    break;
                case "?":
                case "help":
                case "info":
                    var helpcommand = new HelpCommand();
                    helpcommand.Setup(args);
                    consoleCommand = helpcommand;
                    break;
                case "ls":
                case "cd":
                case "cd\\":
                case "cd..":
                case "dir":
                    var lscommand = new LsCommand();
                    lscommand.Setup(args);
                    consoleCommand = lscommand;
                    break;
              
                case "config":
                    var varcommand = new ConfigCommand();
                    varcommand.Setup(args);
                    consoleCommand = varcommand;
                    break;
                case "cls":
                case "clear":
                case "clean":
                    var clscommand = new CleanCommand();
                    clscommand.Setup(args);
                    consoleCommand = clscommand;
                    break;
                case "print":
                case "prn":
                case "echo":
                    var printCommand = new PrintCommand();
                    printCommand.Setup(args);
                    consoleCommand = printCommand;
                    break;
                case "set":
                case "var":
                    Command assigncommand;
                    if (args.Length < 2)
                        assigncommand = new ConfigCommand();
                    else
                        assigncommand = new SetVariableCommand();
                    assigncommand.Setup(args);
                    consoleCommand = assigncommand;
                    break;
                case "exit":
                case "quit":
                case "x":
                case "q":
                    var xcommand = new QuitCommand();
                    xcommand.Setup(args);
                    consoleCommand = xcommand;
                    break;
                case "create":
                case "peek":
                case "read":
                case "enqueue":
                case "dequeue":
                case "push":
                case "pull":
                case "update":
                case "upload":
                case "post":
                case "put":
                case "delete":
                case "select":
                case "insert":
                case "add":
                case "list":
                case "get":
                case "options":
                    var storageCommand = new StorageCommand(isLocalDevelopment);
                    storageCommand.Setup(args);
                    //Dont lenghten the switch here
                    consoleCommand = storageCommand.ResolveCommand();
                    break;
                default:
                    var uscommand = new UsageCommand();
                    uscommand.Setup(args);
                    consoleCommand = uscommand;
                    break;
            }
            return consoleCommand;
        }

    }
}
